#include <regx52.h>
// #include "Digital_tube.h"
#include "DHT11.h"
// #include "oled.h"
#include "LCD1602.h"
#include "Timer0.h"
#include "Delay.h"
#include "xpt2046.h"


sbit soil_H_Threshold=P2^1;
sbit SERVO = P1^7;
sbit beep = P2^2;
//舵机高电平的时间
unsigned char times = 0;
//光敏电阻模拟量阈值
// unsigned int Illumination_threshold = 200;

//空气温湿度
unsigned int temp,humi;


void Timer_0_hander() interrupt 1
{
    SERVO = 0;  // 定时时间到，拉低电平
    TR0 = 0;    // 关闭定时器
}



/**
  * @brief  舵机先转到度数
  * @param  angle 0 - 180
  * @retval 无
  */
void servo_angle(unsigned char angle)
{
    unsigned int pulse_width,timer_val;
    angle = angle % (180+1);
    // 计算所需高电平时间 (500-2500us对应0-180度)
    pulse_width = 500 + angle * (2000 / 180);

    // 转换为定时器计数值 (系统时钟11.0592MHz)
    // 每机器周期=12/11.0592MHz≈1.085us
    // 计数次数 = pulse_width / 1.085
    timer_val = 65536 - (unsigned int)(pulse_width / 1.085);

    // 设置定时器重装值
    TH0 = timer_val >> 8;
    TL0 = timer_val & 0xFF;
    
    SERVO = 1;  // 启动高电平
    // 启动定时器
    TR0 = 1;
}




int key() {
    if(P3_1==0) {
        Delayms(20);
        while (P3_1==0);
        Delayms(20);
        return 1;
    }
    // P3_0 = 1;
    if(P3_0==0) {
        Delayms(20);
        while (P3_0==0);
        Delayms(20);
        return 2;
    }
    P3_2=1;
    if(P3_2==0) {
        Delayms(20);
        while (P3_2==0);
        Delayms(20);
        return 3;
    }
    if(P3_3==0) {
        Delayms(20);
        while (P3_3==0);
        Delayms(20);
        return 4;
    }
    return 0;
}


void mode(void)
{
    unsigned int adc_value=0;
    unsigned char temp=0;
    static unsigned char flag = 0,angle=0;

    temp = key();
    if(0 != temp) flag = temp;
    if(!flag) servo_angle(0);

    adc_value = xpt2046_read_adc_value(0xA4);//测量光敏电阻
    LCD_ShowNum(2,1,flag,1);
    LCD_ShowNum(2,4,angle,3);
    LCD_ShowString(2,9,"ADC:");
    LCD_ShowNum(2,13,adc_value,3);
    
    switch (flag)
    {
    case 1:
        if(soil_H_Threshold == 0)
        {
            servo_angle(180);
            beep = 0;
        }else{
            servo_angle(0);
            angle=0;
            beep = 1;
        }

        break;

    case 2:
        if(adc_value > 200 && (humi/10) < 60)
        {
            servo_angle(adc_value/5);
            LCD_ShowNum(2,4,adc_value/5,3);
            beep = 0;
        }
        else
        {
            servo_angle(0);
            angle=0;
            beep = 1;
        }
        break;

    case 3:
        if(angle < 180) angle++;
        servo_angle(angle);
        (angle == 0) ? (beep = 1) : (beep = 0);
        break;
    case 4:
        if(angle > 0) angle--;
        servo_angle(angle);
        (angle == 0) ? (beep = 1) : (beep = 0);
        break;
    
    }
}


void main(){
    
    LCD_Init();
    Timer0_Init();
    while(1){
        Dht11_Get_Temp_Humi_Value(&temp,&humi);

        LCD_ShowString(1,1,"T:");
        LCD_ShowChar(1,5,'.');
        LCD_ShowChar(1,7,'%');
        LCD_ShowNum(1,3,temp/10,2);
        LCD_ShowNum(1,6,temp%10,1);

        LCD_ShowString(1,10,"H:");
        LCD_ShowChar(1,14,'.');
        LCD_ShowChar(1,16,'%');
        LCD_ShowNum(1,12,humi/10,2);
        LCD_ShowNum(1,15,humi%10,1);
        
        

        
        mode();
    }
}








